What Mr. "I am always right" hasn't realized yet is that his code is buggy, despite all the "pointer leads to bugs" rhetoric. His code:
Code:
void a_reverse(char *buffer, const char *array, int length) {
int a = length;
int b = 0;
while (0 != a) {
a = a - 1;
buffer[b] = array[a];
b = 1 + b;
}
buffer[b] = '\0';
// WHY?!
return;
}
Of course the buffer and array pointers (oh, the horror!) must point to valid memory, but what will happen if length is negative? What will happen if both pointers points to the same memory region? All his effort to "be right and everybody knows it" (obviously a fallacy) vanished right there.
Just because he doesn't feel confortable using pointers and tries to signal virtue... Comparing to his, this is way less buggy AND commented, in case anyone has doubts on what the routine is doing:
Code:
// Using restrict to tell the compiler the pointers cannot be the same.
// Yep... assumes both pointers are valid (notice: no 'length'!).
char *strrev( char * restrict out, const char * restrict in )
{
// points out to the end of the buffer and puts a '\0' there.
out += strlen( in );
*out-- = '\0';
// while '\0' isn't found yet, copy chars in reverse order,
// incrementing the input pointer, and decrementing the output pointer.
while ( *in ) *out-- = *in++;
// puts out back at the beginning and return it.
return ++out;
}
The same assumptions apply: Both arguments must be valid pointers AND not pointing to overlapped regions (as his routine also implies -- but worse!) AND the output buffer must be the same size as the input buffer.
So... yep... keep trying. One day you'll enjoy using pointers...